using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace SurveyImporter
{
	/// <summary>
	/// Summary description for frmChooseLanguage.
	/// </summary>
	public class frmChooseLanguage : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cboLanguages;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOk;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private frmMain m_parent;

		public frmChooseLanguage(frmMain parent)
		{
			InitializeComponent();			
			m_parent = parent;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cboLanguages = new System.Windows.Forms.ComboBox();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOk = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// cboLanguages
			// 
			this.cboLanguages.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.cboLanguages.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboLanguages.Location = new System.Drawing.Point(8, 8);
			this.cboLanguages.Name = "cboLanguages";
			this.cboLanguages.Size = new System.Drawing.Size(176, 21);
			this.cboLanguages.TabIndex = 0;
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(192, 8);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOk
			// 
			this.btnOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btnOk.Location = new System.Drawing.Point(272, 8);
			this.btnOk.Name = "btnOk";
			this.btnOk.TabIndex = 2;
			this.btnOk.Text = "Ok";
			this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
			// 
			// frmChooseLanguage
			// 
			this.AcceptButton = this.btnOk;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(352, 40);
			this.Controls.Add(this.btnOk);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.cboLanguages);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
			this.MinimumSize = new System.Drawing.Size(360, 64);
			this.Name = "frmChooseLanguage";
			this.Text = "Choose Language";
			this.Load += new System.EventHandler(this.frmChooseLanguage_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			m_parent.m_languageID = 0;
			this.Close();
		}

		private void frmChooseLanguage_Load(object sender, System.EventArgs e)
		{
			Top = m_parent.Top;
			Left = m_parent.Left;
			
			SqlConnection conn = new SqlConnection(frmMain.DB_CONN_STR);
			SqlCommand cmd = new SqlCommand();
			SqlDataReader dr = null;

			#region Load Languages
			try
			{
				conn.Open();
				cmd.Connection = conn;				
				cmd.CommandText = "dbo.SurveyAdmin_GetLanguages";
				cmd.CommandType = CommandType.StoredProcedure;				
				dr = cmd.ExecuteReader();

				cboLanguages.Items.Clear();

				while(dr.Read())
					cboLanguages.Items.Add(new ListItem(dr["LanguageID"].ToString(),dr["LanguageName"].ToString()));				
			}								
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			finally
			{	
				if(dr!=null)
				{
					dr.Close();
					dr = null;
				}
				
				conn.Close();
				conn.Dispose();				
				cmd.Parameters.Clear();
				cmd.Dispose();
			}
			#endregion

			cboLanguages.SelectedIndex = 0;
		}

		private void btnOk_Click(object sender, System.EventArgs e)
		{	
			m_parent.m_languageID = Convert.ToInt32(((ListItem)cboLanguages.SelectedItem).Value);
			this.Close();		
		}
	}
}
